import time
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait

class Base:
    # 初始化
    def __init__(self,driver):
        self.driver=driver

    # 封装url
    def open_url(self,url):
        self.driver.get(url)

    # 封装截图
    def error_png(self):
        error_png=time.strftime('%Ym%d-%H%M%S')
        return error_png

    # 封装定位
    def find_element(self,loc):
        try:
            WebDriverWait(self.driver,20,0.5).until(lambda driver:driver.find_element(*loc).is_displayed())
            return self.driver.find_element(*loc)
        except ValueError as e:
            self.driver.save_screenshot('./'+self.error_png()+'.png')
            print(e)

    # 封装点击
    def click(self,loc):
        self.find_element(loc).click()

    # 封装输入
    def send_keys(self,loc,info):
        self.find_element(loc).send_keys(info)

    # 提取文字
    def text(self,loc):
        return self.find_element(loc).text
